<?php
namespace app\kccsCourse\controller;
use app\common\Base;
use app\common\Compent;
use app\common\Excel;
use think\Db;

class CourseClassSignupTotal extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		return $this::getResult();
	}

	public function exportExcel() {
		//excel表头内容
		$fields = $this::getHeader();
		$list = explode(",", $fields);
		$header = [];
		foreach ($list as $key => $value){
			$header[$value] = $value;
		}
		Excel::toExcel($this::getResult(),'课程班级报名统计',$header);
		exit;
	}

	public function getHeader() {
		$grade = input("grade");
		if(!$grade) return "";
		$array = explode("_", $grade);
		$where["年级号"] = $array[0];
		$where["学籍类型"] = $array[1];
		$list = Db::table("学生信息")
				->field("班号 clazz")
				->where($where)
				->group("班号")
				->order("clazz")
				->select();
		$headers = "课程分类,课程编号,课程名称,上课时间";
		$gradeName = getGradeName($where["年级号"],$where["学籍类型"]);
		foreach ($list as $obj) {
			$headers = $headers.",".$gradeName.$obj["clazz"]."班";
		}
		$headers = $headers.",总报名人数,剩余名额";
		return $headers;
	}

	public function getResult() {
		$termId = input("termId");
		$grade = input("grade");
		if(!$grade) return [];
		$gradeArray = explode("_", $grade);
		$where["学籍类型"] = $gradeArray[1];
		$classList = Db::table("学生信息")
				->field("班号 clazz")
				->where($where)
				->group("班号")
				->order("clazz")
				->select();
		$where["IS_VALID"] = 1;
		$where["TERM_ID"] = $termId;
		$totalList = Db::table("t_kccs_student_sign a")
					 ->join("学生信息 b","a.STUDENT_ID = b.ID","inner")
					 ->join("t_kccs_course_info c","c.ID = a.COURSE_ID","inner")
					 ->field("COURSE_ID courseId,SIGN_CLASS_DATE signClassDate,年级号 grade,班号 clazz,COUNT(1) num")
					 ->where($where)
					 ->group("COURSE_ID,SIGN_CLASS_DATE,年级号,班号")
					 ->select();
		$totalMap = [];
		$gradeMap = [];
		foreach ($totalList as $obj) {
			$totalMap[$obj["courseId"].$obj["signClassDate"].$obj["grade"].$obj["clazz"]] = intval($obj["num"]);
			$gradeKey = $obj["courseId"].$obj["signClassDate"];
			if(!array_key_exists($gradeKey, $gradeMap)) {
				$gradeMap[$gradeKey] = intval($obj["num"]);
			}else {
				$total = intval($gradeMap[$gradeKey]);
				$gradeMap[$gradeKey] = $total+intval($obj["num"]);
			}
		}
		$courseList = Db::table("t_kccs_course_info")
					  ->where("IS_AGREE = 1")
					  ->order("COURSE_NO")
					  ->select();
		$result = [];
		$idx = 0;
		foreach ($courseList as $key => $kc) {
			$cs = explode(",",$kc["CLASS_DATE"]);
			foreach ($cs as $c) {
				$item = [];
				$item["课程分类"] = $kc["COURSE_TYPE"];
				$item["课程编号"] = $kc["COURSE_NO"];
				$item["课程名称"] = $kc["COURSE_NAME"];
				$item["上课时间"] = $c;
				foreach ($classList as $clazz) {
					$tkey = $kc["ID"].$c.$gradeArray[0].$clazz["clazz"];
					$num = array_key_exists($tkey, $totalMap) ? $totalMap[$tkey] : 0;
					$item[getGradeName($gradeArray[0],$gradeArray[1]).$clazz["clazz"]."班"] = $num;
				}

				$totalNum = array_key_exists($kc["ID"].$c, $gradeMap) ? $gradeMap[$kc["ID"].$c] : 0;
				$item["总报名人数"] = $totalNum;
				$item["剩余名额"] = intval($kc["LIMIT_NUM"]) - $totalNum;
				$result[$idx] = $item;
				$idx ++;
			}
		}
		return $result;
	}

}
